package org.example.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.netty.util.internal.StringUtil;
import org.example.backend.bean.UserBase;
import org.example.backend.common.Result;
import org.example.backend.common.constt.RedisConst;
import org.example.backend.mapper.UserBaseMapper;
import org.example.backend.service.QQEMailService;
import org.example.backend.utils.CaptchaUtils;
import org.example.backend.utils.JwtUtils;
import org.example.backend.utils.QQEMailUtils;
import org.example.backend.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 啊昌
 * @date 2025/7/19 21:35
 * QQEMailServiceImpl qq邮箱实现类
 */
@Service
public class QQEMailServiceImpl implements QQEMailService {

	@Autowired
	private QQEMailUtils qqEmailUtils;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private CaptchaUtils captchaUtils;
	@Autowired
	private JwtUtils jwtUtils;
	@Autowired
	private UserBaseMapper userBaseMapper;

	/**
	 * 发送QQ邮箱验证码
	 *
	 * @param email 收件人邮箱地址
	 * @return 如果发送成功返回空字符串，否则返回null
	 */
	@Override
	public Result<Map<String,Object>> sendQQEMail(String email) {
		//校验参数
		if(StringUtil.isNullOrEmpty(email)){
			return null;
		}
		//生成六位数的验证码
		String Captcha = captchaUtils.generateCaptcha();
		//根据邮箱查询用户信息
		UserBase userBase = userBaseMapper.selectOne(new QueryWrapper<UserBase>().eq("email", email));
		if(userBase == null){
			//用户不存在
			return null;
		}
		//将验证码存储在redis缓存中 组成 user:captcha:用户id  60*5->300秒过期
		redisUtil.set(RedisConst.GetUserCaptcha(userBase.getUserId()),Captcha,60*5);
		//发送邮件
		qqEmailUtils.sendBilibiliVerificationCode(email,Captcha);
		return Result.success();
	}
}
